<template>
    <div class="goods-contrinal">
       <router-link tag="div" :to="'pohotos/goodsinfo/'+item.id" class="box" v-for="item in goodsList" :key="item.id" >
           <img :src="item.img_url" >
            <h1 class="title">{{item.title}}</h1> 
            <div class="goodsinfo">
                <p class="price">
                    <span class="new-price">￥{{item.sell_price}}</span>
                    <span class="old-price">￥ <del>{{item.market_price}}</del></span>
                </p>
                <p class="info">
                    <span>热卖中</span>
                    <span class="stock">剩余{{item.stock_quantity}}件</span>
                </p>
            </div>
        </router-link>
        <!-- 加载更多按钮 -->
        <van-button class="more-goods" plain hairline type="info" size="large" @click="getMoreGoods">加载更多</van-button>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                index: 1,
                goodsList: [],
                isload: false //节流阀
            }
        },
        created() {
            this.getgoods()
        },
        methods: {
            //获取商品列表数据
            async getgoods() {
                const {
                    data
                } = await this.$http.get('api/getgoods?pageindex=' + this.index)
                if (data.status === 0) {
                    if (data.message.length <= 0) {
                        this.isload = true;
                    }
                    this.goodsList = this.goodsList.concat(data.message)
                        // console.log(data.message);

                }


            },
            //请求加载更多列表
            getMoreGoods() {
                //如果请求内容为空 ,返回不在发送请求
                if (this.isload) return;
                this.index++;
                this.getgoods();
            }
        },
    }
</script>
<style lang="scss">
    .goods-contrinal {
        display: flex;
        flex-wrap: wrap;
        padding: 7px;
        justify-content: space-between;
        .box {
            width: 49%;
            box-shadow: 0 0 5px #999;
            margin: 5px 0;
            padding: 4px;
            border: 1px solid #ccc;
            /* min-height: 294px; */
            background-color: #fff;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            img {
                width: 100%;
                min-height: 170px;
            }
            .title {
                font-size: 14px;
                padding: 0 4px;
            }
            .price {
                .new-price {
                    color: red;
                    font-weight: 600;
                    font-size: 16px;
                }
                .old-price {
                    margin-left: 5px;
                    font-size: 12px;
                }
            }
            .info {
                display: flex;
                justify-content: space-between;
            }
        }
        .more-goods {
            margin: 7px 0;
        }
    }
</style>